package com.ysd.lis.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.lis.entity.SysInterface;
import com.ysd.lis.entity.SysJob;
import com.ysd.lis.mapper.sys.SysInterfaceMapper;
import com.ysd.lis.service.SysJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 标本可接受性
 *
 * @author wanghao
 * @create 2024/7/15 0015
 */
@Component("interface")
public class InterfaceSchedule {

    private static final Logger logger = LoggerFactory.getLogger(InterfaceSchedule.class);

    @Autowired
    private SysJobService sysJobService;
    @Autowired
    SysInterfaceMapper sysInterfaceMapper;

    public void GetAdiconReport() {
        logger.info("艾迪康接口外送报告开始执行");
        String returnMessage = interfaceScheduleUtil("interface.GetAdiconReport");
        logger.info(returnMessage);
    }

    public void GetHuaYinReport() {
        logger.info("华银接口外送报告开始执行");
        String returnMessage = interfaceScheduleUtil("interface.GetHuaYinReport");
        logger.info(returnMessage);
    }

    public void GetKingMedReport() {
        logger.info("金域接口外送报告开始执行");
        String returnMessage = interfaceScheduleUtil("interface.GetKingMedReport");
        logger.info(returnMessage);
    }

    public void GetRuiMeiReport() {
        logger.info("星齐接口外送报告开始执行");
        String returnMessage = interfaceScheduleUtil("interface.GetRuiMeiReport");
        logger.info(returnMessage);
    }

    public void UpdateRuiMeiReport() {
        logger.info("数据视图上传星齐开始执行");
        String returnMessage = interfaceScheduleUtil("interface.UpdateRuiMeiReport");
        logger.info(returnMessage);
    }

    public void UpdateKingMedReport() {
        logger.info("数据视图上传金域开始执行");
        String returnMessage = interfaceScheduleUtil("interface.UpdateKingMedReport");
        logger.info(returnMessage);
    }

    public void autoWswBloodAudit() {
        logger.info("开始执行微生物血培养自动审核定时任务：" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        //调用自动审核微生物血培养结果
        LambdaQueryWrapper<SysInterface> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysInterface::getDelFlag, 0);
        wrapper.eq(SysInterface::getIsEnable, 1);
        wrapper.eq(SysInterface::getOrgId, "1787488360349155329");
        List<SysInterface> sysInterfaces = sysInterfaceMapper.selectList(wrapper);
        List<String> collect = sysInterfaces.stream().filter(o -> Objects.nonNull(o) && org.apache.commons.lang3.StringUtils.isNotBlank(o.getInterfaceUrl())).map(SysInterface::getInterfaceUrl).distinct().collect(Collectors.toList());
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(collect)) {
            /*发布报告相关*/
            for (String webUrl : collect) {
                logger.info("接口地址为：" + webUrl);
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("orgId", "1787488360349155329");
                String release = HttpUtil.createPost("http://" + webUrl + "/public/autoAuditWswBlood").setConnectionTimeout(3000).body(JSONUtil.toJsonStr(jsonObject)).contentType("application/json").execute().body();

            }
        }
    }

    public String interfaceScheduleUtil(String name) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysJob::getInvokeTarget, name).eq(SysJob::getDelFlag, 0);
        SysJob one = sysJobService.getOne(lambdaQueryWrapper);
        String returnMessage = "";
        if (StringUtils.isNotBlank(one.getRequestType()) && "1".equals(one.getRequestType())) {
            returnMessage = HttpUtil.createGet(one.getUrl()).execute().sync().body();
        } else {
            returnMessage = HttpUtil.createPost(one.getUrl()).execute().sync().body();
        }
        return returnMessage;
    }
}
